package cn.com.okgo.callback;

import java.io.File;

import cn.com.okgo.convert.FileConvert;
import cn.com.okgo.utils.Exceptions;
import okhttp3.Response;
import okhttp3.ResponseBody;


/**
 * Created by luwenlong on 2017/5/15.
 *
 * 返回文件
 */

public abstract class HttpFileCallback extends HttpCallback<File> {

    private FileConvert convert;    //文件转换类

    private String fileDir;
    private String fileName;

    public HttpFileCallback(String fileDir, String fileName) {
        this.fileDir=fileDir;
        this.fileName=fileName;
        convert = new FileConvert(fileDir, fileName);
        convert.setCallback(this);
    }


    @Override
    public File parseNetworkResponse(Response response){
        File file=null;
        try {
            file = convert.convertSuccess(response);
        }catch (Exception e){
            File file1=new File(fileDir,fileName+".tmp");
            file1.delete();
            Exceptions.illegalArgument(e.toString());
        }
        response.close();
        return file;
    }

    @Override
    public File parseCacheResponse(ResponseBody responseBody) throws Exception {
        return null;
    }
}
